﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelixPieceCreator : MonoBehaviour
{
    private const int PIECE_COUNT = 11;


    public float RotateSpeed=10f;

    //正常的平台和死亡的平台
    private GameObject _piecePrefab;
    private GameObject _deadPiecePrefab;
    //down标签的位置
    private Transform _downTransform;

    [SerializeField] private GameObject m_Items;

    //所有的小块
    private List<GameObject> _pieceList = new List<GameObject>();

    //是否是第一个平台
    public bool IsStartPiece;

    private void OnEnable()
    {
        MoveController.Move += Move;
    }

    private void OnDisable()
    {
        MoveController.Move -= Move;
    }

    private void Start()
    {
        _piecePrefab = Resources.Load<GameObject>("PiecePrefab");
        _deadPiecePrefab = Resources.Load<GameObject>("DeadPiecePrefab");
        _downTransform = GameObject.FindGameObjectWithTag("Down").transform;

        if (!IsStartPiece)
        {
            CreatePieces();
            //Random angle for helix
            transform.rotation = Quaternion.Euler(0, Random.Range(0, 180f), 0);
        }
        
    }
    //移动
    private void Move()
    {
        if (!Ball.IsGameOver)
        {
            Quaternion v  = transform.rotation;
            transform.Rotate(Vector3.up, -Input.GetAxis("Mouse X") *10f  , Space.World);
            transform.rotation = Quaternion.Lerp ( v , transform.rotation,Time.deltaTime*RotateSpeed*5 );
        }
    }

    //创建所有方块
    private void CreatePieces()
    {
        float zAngeValue = 0;

        //Create all piece
        for (int i = 0; i < PIECE_COUNT; i++)
        {
            GameObject piece = Instantiate(_piecePrefab, transform, false);
            piece.transform.rotation = Quaternion.Euler(90f, 0, zAngeValue);
            zAngeValue += 30f;
            _pieceList.Add(piece);
        }
        if(m_Items!=null)
            Instantiate ( m_Items , this.transform.position , Quaternion.identity , this.transform );
        //移除方块
        RemovePiece (Random.Range(0, 5));
    }

    //移除平台
    private void RemovePiece(int value)
    {
        switch (value)
        {
            //Remove 2 piece
            case 0:
                for (int i = 0; i < 2; i++)
                {
                    Destroy(_pieceList[i]);
                }
                break;

            //Remove 4 piece
            case 1:
                for (int i = 0; i < 4; i++)
                {
                    Destroy(_pieceList[i]);
                }
                break;

            //Remove 6 piece
            case 2:
                for (int i = 0; i < 6; i++)
                {
                    Destroy(_pieceList[i]);
                }
                break;

            //Remove "triangle" piece
            case 3:
                Destroy(_pieceList[0]);
                Destroy(_pieceList[1]);
                Destroy(_pieceList[4]);
                Destroy(_pieceList[5]);
                Destroy(_pieceList[8]);
                Destroy(_pieceList[9]);
                break;

            //Remove 3 parallel piece
            case 4:
                Destroy(_pieceList[0]);
                Destroy(_pieceList[1]);
                Destroy(_pieceList[2]);
                Destroy(_pieceList[6]);
                Destroy(_pieceList[7]);
                Destroy(_pieceList[8]);
                break;
        }

        CreateDeadPice();
    }
    //创建死亡方块
    private void CreateDeadPice()
    {
        //Create dead piece
        int a = 0;
        if (ScoreManager.Instance._scoreValue <= 30)
            a = 1;
        else if (ScoreManager.Instance._scoreValue <= 60)
            a = 2;
        else
            a = 3;


        for (int i = 0; i < a ; i++)
        {
            GameObject deadPiece = Instantiate(_deadPiecePrefab, transform, false);
            deadPiece.transform.rotation = Quaternion.Euler(90f, 0, (Random.Range(0, 12) * 30f));
            
        }
    }


    //删除所有方块
    private void DestroyAllPieces()
    {
        foreach (Transform child in transform)
        {
            _pieceList.Clear ();
            Destroy ( child.gameObject );
        }
    }

    //重制平台
    public void ResetNewPiece()
    {
        DestroyAllPieces();
        Vector3 currentPos = GameManager.Instance.CurrentPlanePos;
        currentPos.y -= 2.5f;
        transform.position = currentPos;
        GameManager.Instance.CurrentPlanePos = transform.position;

        CreatePieces ();
    }
    //出界后重制
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Up")
        {
            this.gameObject.tag = "HelixPlatform";
            ResetNewPiece ();
        }
    }

    
}
